function quickSort(arr) {
    if (arr.length <= 1) return arr
    let middle = arr[0]
    const left = []
    const right = []

    for (let i = 0; i < arr.length; i++) {
        const element = arr[i];
        if (element > middle) right.push(element)

        if (element < middle) left.push(element)
    }
    return [...quickSort(left), middle, ...quickSort(right)]
}
let arr = [3, 6, 2, 4, 1]

console.log(quickSort(arr))